﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SisVet.VendasAvulsasModelo;
using SisVet.Utils;

namespace SisVet.VendasAvulsas
{
    public partial class frmVendasAvulsas : Form
    {
        VendasAvulsasDAL vendaAvulsa = new VendasAvulsasDAL();
        public frmVendasAvulsas()
        {
            InitializeComponent();
        }   
        
        private void btnSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                vendaAvulsa.LancarVendaAvulsa(Convert.ToInt32(txtCodigoServico.Text), Convert.ToInt32(txtQtdServico.Text), Convert.ToDouble(txtValorTotal.Text));
                grdVendasAvulsas.DataSource = vendaAvulsa.ListarVendasAvulsas();

                Utils.Utils.LimparCampos(this);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ocorreu um erro ao incluir o serviço: ", ex.ToString());
            }
        }

        private void txtCodigoServico_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (vendaAvulsa.ExisteServico(Convert.ToInt32(txtCodigoServico.Text)))
                {
                    Servicos servico = vendaAvulsa.BuscarServicoPorId(Convert.ToInt32(txtCodigoServico.Text));
                    txtDescricaoServico.Text = servico.NmServico;
                    txtPrecoUnitServico.Text = servico.Valor.ToString();
                }
                else
                    MessageBox.Show("Serviço não encontrado!");
               
            }
        }

        private void txtQtdServico_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                AtualizarValorTotal();

        }

        private void AtualizarValorTotal()
        {
            int qtdVenda = Convert.ToInt32(txtQtdServico.Text);
            double precoUnitario = Convert.ToDouble(txtPrecoUnitServico.Text);

            txtValorTotal.Text = Convert.ToString(qtdVenda * precoUnitario);
        }

        

        private void txtQtdServico_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == 8);
        }

        private void btnPesquisarServico_Click(object sender, EventArgs e)
        {            
            frmPesquisa telaPesquisa = new frmPesquisa();
            telaPesquisa.ShowDialog();            
            txtCodigoServico.Text = telaPesquisa.CodServico;
            txtDescricaoServico.Text = telaPesquisa.NmServico;
            txtPrecoUnitServico.Text = telaPesquisa.Vlunitario;
            telaPesquisa.Close();

        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmVendasAvulsas_Load(object sender, EventArgs e)
        {
  
        }       
    }
}
